package org.dromara.activity.rest;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.activity.domain.bo.SchoolBo;
import org.dromara.activity.domain.vo.SchoolVo;
import org.dromara.activity.service.SchoolService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.UserConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 活动学校
 * @author Administrator
 * @date 2024/01/16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/activity/school")
public class SchoolRest extends BaseController {


    private final SchoolService schoolService;


    /**
     * 获取学校信息列表
     */
    @SaCheckPermission("system:school:list")
    @GetMapping("/list")
    public TableDataInfo<SchoolVo> list(SchoolBo school, PageQuery pageQuery) {
        return schoolService.selectPageSchoolList(school, pageQuery);
    }



    /**
     * 新增学校
     */
    @SaCheckPermission("system:school:add")
    @Log(title = "学校管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody SchoolBo school) {
        if (!schoolService.checkSchoolNameUnique(school)) {
            return R.fail("新增学校'" + school.getSchoolName() + "'失败，学校名称已存在");
        }
        return toAjax(schoolService.insertSchool(school));
    }

    /**
     * 修改学校
     */
    @SaCheckPermission("system:school:edit")
    @Log(title = "学校管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody SchoolBo school) {
        if (!schoolService.checkSchoolNameUnique(school)) {
            return R.fail("修改学校'" + school.getSchoolName() + "'失败，学校名称已存在");
        }
        return toAjax(schoolService.updateSchool(school));
    }


    /**
     * 状态修改
     */
    @SaCheckPermission("system:school:edit")
    @Log(title = "学校管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SchoolBo school) {
        return toAjax(schoolService.updateSchoolStatus(school.getId(), school.getStatus()));
    }

    /**
     * 删除学校
     *
     * @param ids 学校ID串
     */
    @SaCheckPermission("system:school:remove")
    @Log(title = "学校管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(schoolService.deleteSchoolByIds(ids));
    }
}
